﻿using B.Auth.APi.Application.Command.RoleUsers;
using B.Auth.APi.Application.Command.RoleUsers.Dto;
using B.Auth.APi.Application.Command.Users;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand.RoleUsers
{
    public class GetUserNameTreeCommandHandler : IRequestHandler<GetUserNameTreeCommand, ApiResult<List<GetUserTreeDto>>>
    {

        private readonly IBaseRepository<User> _user;

        public GetUserNameTreeCommandHandler(IBaseRepository<User> user)
        {
            _user = user;
        }

        public async Task<ApiResult<List<GetUserTreeDto>>> Handle(GetUserNameTreeCommand request, CancellationToken cancellationToken)
        {
          var list=_user.GetAll().Where(x=>x.departid==request.partid).ToList();
            var result=new ApiResult<List<GetUserTreeDto>>();
            List<GetUserTreeDto> userTreeDtos = new List<GetUserTreeDto>();
            foreach (var item in list)
            {

                userTreeDtos.Add(new()
                {

                    id = item.Id,
                    label = item.Name,
                    children = [],

                });

                
            }
            result.data= userTreeDtos;

            return await Task.FromResult(result);




        }

       

    }
}
